/*
 * Copyright (c) 2014 Luca Agostini, Alessandro Cantini, Alessandro Dignani
 *
 * This file is part of MeteoCal.
 *
 * MeteoCal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.polimi.se2.agostini_cantini_dignani.meteocal.gui;

import it.polimi.se2.agostini_cantini_dignani.meteocal.business.EventManager;
import it.polimi.se2.agostini_cantini_dignani.meteocal.business.UserManager;
import it.polimi.se2.agostini_cantini_dignani.meteocal.business.WeatherForecastManager;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.Event;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.User;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.WeatherForecast;
import org.primefaces.context.RequestContext;
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.Date;

/**
 * @author Alessandro Cantini <alessandro.cantini@mail.polimi.it>
 */
@Named
@ViewScoped
public class CalendarBean extends DateTimeBean {

	private ScheduleModel model;
	private ScheduleEventImpl selectedEvent;
	private ScheduleEventImpl currentScheduleEvent;
	private Event dataEvent;

	@EJB
	private UserManager um;

	@EJB
	private EventManager em;

	@EJB
	private WeatherForecastManager wfm;

	/**
	 * Creates the model using the calendar of the logged user. A default selectedEvent with id = 0
	 * is created in order to avoid NullPointerException during the creation of the page.
	 */
	@PostConstruct
	public void init() {
		super.init();
		User loggedUser = um.getLoggedUser();
		model = createScheduleModel();

		Event e = new Event();
		Date now = new Date();
		e.setStartTime(now);
		e.setEndTime(new Date(now.getTime() + 1));
		e.setEventId(0);
		e.setWeatherForecast(new WeatherForecast());
		e.getWeatherForecast().setIconId("01d");
		e.getWeatherForecast().setTemperature(0.1f);
		selectedEvent = new ScheduleEventImpl(loggedUser.getCalendar(), e, loggedUser);
	}

	protected ScheduleModelImpl createScheduleModel() {
		return new ScheduleModelImpl(um.getLoggedUser());
	}

	public void onEventSelect(SelectEvent selectEvent) {
		selectedEvent = (ScheduleEventImpl) selectEvent.getObject();
	}

	public String removeParticipation() {
		Event e = em.findById(selectedEvent.getEventId());
		em.removeParticipation(e, um.getLoggedUser().getCalendar());
		init();
		return null;
	}

	public void onEventMove(ScheduleEntryMoveEvent seme) {
		checkOverlappings(seme.getScheduleEvent());
	}

	public void onEventResize(ScheduleEntryResizeEvent sere) {
		checkOverlappings(sere.getScheduleEvent());
	}

	public ScheduleModel getModel() {
		return model;
	}

	public ScheduleEventImpl getSelectedEvent() {
		return selectedEvent;
	}

	public void saveAction() {
		dataEvent.setStartTime(currentScheduleEvent.getStartDate());
		dataEvent.setEndTime(currentScheduleEvent.getEndDate());
		currentScheduleEvent.updateEvent(dataEvent);
		em.editEvent(dataEvent, true);
	}

	public void cancelUpdate() {
		currentScheduleEvent.updateEvent(dataEvent);
	}

	public String getIconUrl() {
		return "http://openweathermap.org/img/w/" + selectedEvent.getWeatherForecast().getIconId() + ".png";
	}

	public String getBackgroundPath() {
		if (selectedEvent.getWeatherForecast() != null) {
			String icon = selectedEvent.getWeatherForecast().getIconId().replace("n", "d");
			return "../images/meteopics/" + icon + ".jpg";
		}
		return "../images/meteopics/blank.png";
	}

	public String getTextColor() {
		if (selectedEvent.getWeatherForecast() != null) {
			return "#fff";
		} else {
			return "#444";
		}
	}

	public String getTemperature() {
		return wfm.getTemperatureString(selectedEvent.getWeatherForecast(), um.getLoggedUser().getTemperatureUnit());
	}

	private void checkOverlappings(ScheduleEvent se) {
		currentScheduleEvent = (ScheduleEventImpl) se;
		dataEvent = em.findById(currentScheduleEvent.getEventId());

		Event newEvent = new Event();
		newEvent.setStartTime(currentScheduleEvent.getStartDate());
		newEvent.setEndTime(currentScheduleEvent.getEndDate());
		newEvent.setEventId(currentScheduleEvent.getEventId());

		if (em.thereAreOverlappingEvents(newEvent, um.getLoggedUser().getCalendar())) {
			RequestContext.getCurrentInstance().execute("PF('calendarConfirmDialog').show()");
		} else {
			saveAction();
		}
	}
}

